﻿// See https://aka.ms/new-console-template for more information
using FieldTools.Util;
using System.Net;
using System.Reflection;
using TestConsole;

namespace TestConsole
{
    public class Program
    {
        public static void Main()
        {
            //AttributeTestClass.GetAttribute(typeof(AttributeTestClass));
            TestClass2 t2 = new() { MyProperty = 1, MyProperty1 = 2, MyProperty2 = "hahaha" };
            TestClass3 t3 = new() { MyProperty = 1, MyProperty1 = 2, MyProperty2 = "hahaha" };
            //Excute(t3);
            Console.WriteLine("t1");
            Excute2(t2);
            Console.WriteLine("t2");
            Excute2(t3);
        }

        static void Excute(object obj)
        {
            //获取对象属性名
            foreach (PropertyInfo item in obj.GetType().GetProperties())
            {
                Console.WriteLine("属性名:" + item.Name);
                Console.WriteLine("属性类型:" + item.GetValue(obj)?.GetType().ToString());
                //Console.WriteLine("属性值:" + t2.GetPropertyValue(item.Name));
                Console.WriteLine("属性值:" + item.GetValue(obj));
                var attributes = item.GetCustomAttributes(true);
                //遍历所有特性
                foreach (var attribute in attributes)
                {
                    //Console.WriteLine(item.Name+"具有自定义属性如下:");
                    //Console.WriteLine("类型为:"+attribute.GetType().Name);
                    //Console.WriteLine("实例序列化为:" + attribute.ToJson());
                    //判断具备某个特性
                    if (attribute.GetType().Name == nameof(Save2Attribute))
                    {
                        Console.WriteLine("class:" + obj.GetType().FullName);
                        Console.WriteLine("name:" + item.Name);
                    }
                }
            }
        }
        static void Excute2(object obj)
        {
            var ats =  Attribute.GetCustomAttributes(obj.GetType());
            foreach (var at in ats)
            {
                Console.WriteLine(at.GetType().Name);
            }
        }

        /// <summary>
        /// 从gitee中下载发行版本（非接口，xml解析）
        /// </summary>
        void DLGitee()
        {
            Console.WriteLine("作者：SoloShine");
            Console.WriteLine("邮箱：soloshine.cfh@foxmail.com");
            Console.WriteLine("功能：下载gitee网站中指定开源软件的发行版");
            Console.WriteLine("版本：0.0.1");
            Console.WriteLine("更新时间：2022/04/07");
            Console.WriteLine("备注：仅能下载作者手动打包的文件，系统自动打包的源码无法下载（需要验证，也没必要）");

            //Console.WriteLine(args);
            var jsonStr = File.ReadAllText("config.json");
            var config = jsonStr.ToObject<Config>();
            TestClass testClass = new();
            testClass.Testc(config);
            Console.ReadKey();
        }

        #region 验证
        [Test("hahaha", "2")]
        public class AttributeTestClass()
        {
            public int MyProperty { get; set; } = 1;
            public int MyProperty1 { get; set; } = 2;

            public static void GetAttribute(Type t)
            {
                // Get instance of the attribute.
                TestAttribute MyAttribute =
                    (TestAttribute)Attribute.GetCustomAttribute(t, typeof(TestAttribute));

                if (MyAttribute == null)
                {
                    Console.WriteLine("The attribute was not found.");
                }
                else
                {
                    // Get the Name value.
                    Console.WriteLine("The Name Attribute is: {0}.", MyAttribute.Name);
                    // Get the Level value.
                    Console.WriteLine("The Level Attribute is: {0}.", MyAttribute.Level);
                    // Get the Reviewed value.
                    Console.WriteLine("The Reviewed Attribute is: {0}.", MyAttribute.Reviewed);
                }
            }
        }

        #endregion
    }
}
